package sixth.group.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sixth.group.dao.*;
import sixth.group.domain.*;
import sixth.group.service.XyxAdministratorService;

import java.util.List;

@Service(value = "xyxAdministratorService")
public class XyxAdministratorServiceImpl implements XyxAdministratorService {

    @Autowired
    private AdministratorMapper administratorMapper;
    @Autowired
    private OrderinfMapper orderinfMapper;
    @Autowired
    private RestaurantMapper restaurantMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private LogsMapper logsMapper;

    //验证登录
    @Override
    public boolean administratorLoginCheck(Administrator administrator) {
        boolean flag = false;
        AdministratorExample administratorExample = new AdministratorExample();
        AdministratorExample.Criteria criteria = administratorExample.createCriteria();
        criteria.andAusernameEqualTo(administrator.getAusername()).andApassEqualTo(administrator.getApass());
        List<Administrator> administrators = administratorMapper.selectByExample(administratorExample);
        if (administrators.size() > 0) {

            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }

    //密码修改
    @Override
    public boolean changeAdministratorPass(Administrator administrator) {
        boolean flag = false;
        AdministratorExample administratorExample = new AdministratorExample();
        AdministratorExample.Criteria criteria = administratorExample.createCriteria();
        criteria.andAusernameEqualTo(administrator.getAusername());
        List<Administrator> administrators = administratorMapper.selectByExample(administratorExample);
        if (administrators.size() > 0) {
            administratorMapper.updateByExampleSelective(administrator, administratorExample);
            flag = true;

            Logs log = new Logs();
            log.setLrole(3);
            log.setLusername(administrator.getAusername());
            log.setLoperation("管理员密码更改");
            log.setLtype("信息更新");
            int logNum = logsMapper.insertSelective(log);
            if (logNum == 0) System.out.println("记入日志表失败");

        } else {
            flag = false;
        }
        return flag;
    }

    //查看订单
    @Override
    public List<Orderinf> scanOrderInf() {
        List<Orderinf> orderinfs = orderinfMapper.selectByExample(null);
        return orderinfs;
    }


    //删除订单
    @Override
    public boolean deleteOrder(Orderinf orderinf) {
        boolean flag = false;
        OrderinfExample orderinfExample = new OrderinfExample();
        OrderinfExample.Criteria criteria = orderinfExample.createCriteria();
        criteria.andOidEqualTo(orderinf.getOid());
        orderinfMapper.deleteOrderDishesByOrderId(orderinf.getOid());
        int f = orderinfMapper.deleteByExample(orderinfExample);
        if (f > 0) {
            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }

    //修改商家权限
    @Override
    public boolean changeRestaurantPermission(Restaurant restaurant) {
        boolean flag = false;
        RestaurantExample restaurantExample = new RestaurantExample();
        RestaurantExample.Criteria criteria = restaurantExample.createCriteria();
        criteria.andRusernameEqualTo(restaurant.getRusername());
        List<Restaurant> restaurants = restaurantMapper.selectByExample(restaurantExample);
        if (restaurants.size() > 0) {
            restaurantMapper.updateByExampleSelective(restaurant, restaurantExample);
            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }

    //修改顾客权限
    @Override
    public boolean changeCustomerPermission(Customer customer) {
        boolean flag = false;
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCusernameEqualTo(customer.getCusername());
        List<Customer> customers = customerMapper.selectByExample(customerExample);
        if (customers.size() > 0) {
            customerMapper.updateByExampleSelective(customer, customerExample);
            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }


}
